package de.ipk_gatersleben.bit.bi.edal.publication.metadata;

import java.io.FileInputStream;
import java.nio.file.Path;

import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

import de.ipk_gatersleben.bit.bi.edal.rmi.client.ClientPrimaryDataFile;

public class FileStoreSwingWorker extends SwingWorker<Object, Object> {

	private Path file;
	private ClientPrimaryDataFile clientPrimaryDataFile;
	private JProgressBar fileProgressBar;

	public FileStoreSwingWorker(JProgressBar fileProgressbar, Path file,
			ClientPrimaryDataFile clientPrimaryDataFile) {

		this.clientPrimaryDataFile = clientPrimaryDataFile;
		this.file = file;
		this.fileProgressBar = fileProgressbar;

	}

	@Override
	protected Object doInBackground() throws Exception {
		try {
			
			this.fileProgressBar.setString(this.file.getFileName().toString());
			
			FileProgressInputStream progressInputStream = new FileProgressInputStream(
					this.fileProgressBar, new FileInputStream(
							this.file.toFile()));

			clientPrimaryDataFile.store(progressInputStream);

			progressInputStream.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void done() {
		this.fileProgressBar.setValue(100);
	}

}